<template>
  <div :style="{height:height2,width:width}" >
    <el-row >
      <el-col :span="5"><div class="block">
    <span class="demonstration">年</span>
    <el-date-picker
      v-model="queryParams.year"
      type="year"
      placeholder="选择年">
    </el-date-picker>
  </div>
  </el-col>
  <el-col :span="5">
    <el-select v-model="queryParams.chickenNo" @change="queryData" placeholder="请选择肉鸡信息">
          <el-option-group
              v-for="group in checkOptions"
              :key="group.label"
              :label="group.label">
              <el-option
                v-for="item in group.options"
                :key="item.value"
                :label="item.label"
                :value="item.label">
              </el-option>
          </el-option-group>
    </el-select>
  </el-col>

</el-row>
<el-row >
  <div :class="className" :style="{height:height,width:width}"  ref="box" >
  
</div>
</el-row>
</div>
</template>

<script>
import { getMonthSumGrowData,getOptionCheckselect } from "@/api/index";
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    height2: {
      type: String,
      default: '450px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      checkOptions: [],
      queryParams: {
        chickenNo: '',
        year: '2024',
      },
      options: [{
          value: '选项1',
          label: '黄金糕'
        }, {
          value: '选项2',
          label: '双皮奶'
        }, {
          value: '选项3',
          label: '蚵仔煎'
        }, {
          value: '选项4',
          label: '龙须面'
        }, {
          value: '选项5',
          label: '北京烤鸭'
        }],
      chart: null,
      myChartData:{},
      value3: '',

    }
  },
  created() {
    this.initData();
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initData(){
      getOptionCheckselect().then(response=>{
      this.checkOptions=response.data;
    });
  
    },
    queryData(){
      getMonthSumGrowData(this.queryParams).then(response=>{
      this.myChartData={
        "expectedData":response.data["resList"][0]["data"],
        "actualData":response.data["resList"][1]["data"],
      };
      console.log( this.myChartData)
        this.setOptions(this.myChartData)
    });
    },

    initChart() {
      this.chart = echarts.init(this.$refs.box, 'macarons')
       this.setOptions()
    },
    setOptions({ expectedData, actualData } = {}) {
      this.chart.setOption({
        title: {
            text: '肉鸡'+this.queryParams.chickenNo+'成长信息'
          },
        xAxis: {
          data: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'],
          boundaryGap: false,
          axisTick: {
            show: false
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          axisTick: {
            show: false
          }
        },
        legend: {
          data: ['肉鸡数据', '平均数据']
        },
        series: [{
          name: '肉鸡数据', itemStyle: {
            normal: {
              color: '#FF005A',
              lineStyle: {
                color: '#FF005A',
                width: 2
              }
            }
          },
          smooth: true,
          type: 'line',
          data: expectedData,
          animationDuration: 2800,
          animationEasing: 'cubicInOut'
        },
        {
          name: '平均数据',
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: '#3888fa',
              lineStyle: {
                color: '#3888fa',
                width: 2
              },
              areaStyle: {
                color: '#f3f8ff'
              }
            }
          },
          data: actualData,
          animationDuration: 2800,
          animationEasing: 'quadraticOut'
        }]
      })
    }
  }
}
</script>

<style  lang="scss" scoped>
 .demonstration {
  padding-right: 10px;
}
</style>
